from PyQt6.QtWidgets import QWidget, QVBoxLayout, QLineEdit, QPushButton, QButtonGroup, QRadioButton


class MotionControllerInterface(QWidget):
	def __init__(self, text: str, control, parent=None):
		super().__init__(parent)
		self.control = control
		self.initUI()
		self.setObjectName(text.replace(' ', '-'))

	def initUI(self):
		layout = QVBoxLayout()

		# Input field for angle
		self.angle_input = QLineEdit(self)
		layout.addWidget(self.angle_input)

		# Button to rotate motor to specified angle
		self.rotate_button = QPushButton('Rotate to Angle', self)
		self.rotate_button.clicked.connect(self.rotate_to_angle)
		layout.addWidget(self.rotate_button)

		# Radio buttons for selecting a position from 0 to 11
		self.position_buttons = QButtonGroup(self)
		for i in range(16):
			button = QRadioButton(str(i), self)
			self.position_buttons.addButton(button, i)
			layout.addWidget(button)

		# Button to rotate motor to selected position's angle
		self.position_button = QPushButton('Rotate to Position', self)
		self.position_button.clicked.connect(self.rotate_to_position)
		layout.addWidget(self.position_button)

		self.setLayout(layout)


	def rotate_to_angle(self):
		angle = self.angle_input.text()
		print(f"Rotating motor to angle: {angle}")
		try:
			self.control.move_motor_to_angle(float(angle))
		except:
			print("Invalid angle value")


	def rotate_to_position(self):
		for i in range(16):
			button = self.position_buttons.button(i)
			if button.isChecked():
				print(f"RadioButton {i} is selected.")
				try:
					self.control.move_motor_to_index(int(i))
				except:
					print("Invalid position value")
	# Add your code here to rotate the motor to the selected position's angle